﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Radiation___IM.Users;

namespace Radiation___IM.Data
{
    public class DatabaseLoader
    {
        public static void Load()
        {
            LoadPermissionGroups();

            if (!Directory.Exists("users"))
                Directory.CreateDirectory("users");

            LoadUsers();
        }

        private static void LoadPermissionGroups()
        {
            if (System.IO.File.Exists("perm_groups.dat"))
                Users.PermissionGroupManager.Instance.setFile((new FileWorker()).processRead("perm_groups.dat"));
            else
            {
                // Default permissions creation

                PermissionGroupManager.Instance.addGroup(new PermissionGroup("admin"));
                PermissionGroupManager.Instance["admin"].addRight("full");

                PermissionGroupManager.Instance.addGroup(new PermissionGroup("guest"));
                PermissionGroupManager.Instance["admin"].addRights(new string[] {
                                                "create_channel",
                                                "change_nick",
                                                "login",
                                                "register",
                                            });

                PermissionGroupManager.Instance.addGroup(new PermissionGroup("member"));
                PermissionGroupManager.Instance["admin"].addRights(new string[] {
                                                "manage_friends",
                                                "create_channel",
                                                "protect_channel",
                                                "change_nick",
                                                "login",
                                                "register",
                                            });
            }
        }

        private static void LoadUsers()
        {
            string[] user_files = Directory.GetFiles("users");
            FileWorker worker = new FileWorker();

            for (int i = 0, j = user_files.Length; i < j; i++)
            {
                File user = worker.processRead(user_files[i]);

                Users.User final_user = new Users.User();
                final_user.setFile(user);

                Users.UserManager.Instance.addUser(final_user, true);
            }

            Users.UserManager.Instance.setNumberOfUsers(user_files.Length);

            if (Users.UserManager.Instance["admin"] == null)
            {
                // Create the admin user
                User user = new User();
                user.Nickname = "admin";
                user.Password = "$admin123";
                user.PermissionGroup = "admin";
                UserManager.Instance.addUser(user);
            }
        }
    }
}
